package com.yic.framework.common.util.object;

import com.yic.framework.common.annotation.FieldMeta;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CompareObjectUtils
 * @Description 对象比较工具类
 * @Author Lijx
 * @Date 2023/11/8 13:52
 * @Version 1.0
 */
@Component
public class CompareObjectUtils {

    private static CompareObjectUtils compareObjectUtils;

    @PostConstruct
    public void init() {
        compareObjectUtils = this;
    }
    /**
     * 获取两个对象同名属性内容不相同的列表
     * @param class1 对象1(旧对象)
     * @param class2 对象2（新对象）
     * @return
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     */
    public static List<Map<String, Object>> compareTwoClass(Object class1, Object class2) throws ClassNotFoundException, IllegalAccessException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        //获取对象的class
        Class<?> clazz1 = class1.getClass();
        Class<?> clazz2 = class2.getClass();
        //获取对象的属性列表
        Field[] field1 = clazz1.getDeclaredFields();
        Field[] field2 = clazz2.getDeclaredFields();
        //遍历属性列表field1
        for (int i = 0; i < field1.length; i++) {
            if(field1[i].isAnnotationPresent(FieldMeta.class))
                //遍历属性列表field2
                for (int j = 0; j < field2.length; j++) {
                    //如果field1[i]属性名与field2[j]属性名内容相同
                    if (field1[i].getName().equals(field2[j].getName())) {
                        field1[i].setAccessible(true);
                        field2[j].setAccessible(true);
                        //如果field1[i]属性值与field2[j]属性值内容不相同
                        if (!compareTwo(field1[i].get(class1), field2[j].get(class2)) && field1[i].isAnnotationPresent(FieldMeta.class) && field2[j].isAnnotationPresent(FieldMeta.class)) {
                            FieldMeta metaAnnotation = field1[i].getAnnotation(FieldMeta.class);
                            Map<String, Object> map2 = new HashMap<String, Object>();
                            map2.put("name", metaAnnotation.name());
                            map2.put("old", field1[i].get(class1) == null ? "" : field1[i].get(class1) );
                            map2.put("new", field2[j].get(class2));
                            list.add(map2);
                        }
                        break;
                    }
                }
        }
        return list;
    }

    public static String compareTwoClass01(Object class1, Object class2) throws ClassNotFoundException, IllegalAccessException {
        List<Map<String, Object>> list = compareTwoClass(class1, class2);
        String content = "";
        for (Map<String, Object> map : list) {
            if (map.get("old") == null) {
                map.put("old", "无");
            }
            content += map.get("name") + ":" + map.get("old") + " 变更为 " + map.get("new") + "；";
        }
        return content;
    }

    /**
     * 对比两个数据是否内容相同
     * @param object1
     * @param object2
     * @return
     */
    public static boolean compareTwo(Object object1, Object object2) {

        if (object1 == null && object2 == null) {
            return true;
        }
        if (object1 == null && object2 != null) {
            return false;
        }
        if (object1.equals(object2)) {
            return true;
        }
        return false;
    }
}
